package cn.message.service.impl;

import cn.itcast.mapper.adminMapper;
import cn.itcast.mapper.permissionMapper;
import cn.itcast.mapper.roleMapper;
import cn.itcast.pojo.*;
import cn.message.service.IRoleService;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author 郭智超
 * @create 2020-04-27 9:43
 */
@Service
@Slf4j
public class RoleServiceImpl implements IRoleService {

    @Resource
    private roleMapper roleMapper;

    @Resource
    private permissionMapper permissionMapper;






    /**
     *
     * @Description:
     * 根据角色获取角色id
     * @auther: 张松
     * @date: 9:47 2020/6/19
     * @param: []
     * @return: java.lang.Integer
     *
     */

    @Override
    public Integer getRoleId(String role) {
        Integer roleId = roleMapper.getRoleId(role);
        return roleId;
    }




    /**
     * 根据管理员的id查询该管理员所拥有的角色
     * @param adminId
     * @return
     */
    @Override
    public List<role> getRoleByAdminId(Integer adminId) {

        return roleMapper.getRoleByAdminId(adminId);

    }

    /**
     * 根据角色名查询角色信息，判断该角色是否存在,不存在返回true，存在返回false
     * @param roleName
     * @return
     */
    @Override
    public Boolean getRoleByRoleName(String roleName) {
        roleExample roleExample = new roleExample();
        cn.itcast.pojo.roleExample.Criteria criteria = roleExample.createCriteria();
        criteria.andRoleNameEqualTo(roleName);
        List<role> roleList = roleMapper.selectByExample(roleExample);
        if(roleList.size() == 0){
            return true;
        }
        return false;
    }

    /**
     * 获取所有的角色信息
     * @param map
     * @return
     */
    @Override
    public Map<String, Object> getAllRole(Map<String, String> map) {
        //获取前台传递的数据
        Integer page = Integer.parseInt(map.get("page"));
        Integer limit = Integer.parseInt(map.get("limit"));
        String roleName = map.get("roleName");

        //添加查询条件
        roleExample roleExample = new roleExample();
        cn.itcast.pojo.roleExample.Criteria criteria = roleExample.createCriteria();
        if (!roleName.isEmpty()){
            criteria.andRoleNameLike("%"+roleName+"%");
        }
        //获取总记录数
        int count = roleMapper.selectByExample(roleExample).size();
        //开始分页
        PageHelper.startPage(page,limit);
        List<role> roleList = roleMapper.selectByExample(roleExample);

        for (role role : roleList) {
            //根据角色的id查询管理员所有的权限id
            List<Integer> permissionIds = roleMapper.getPermissionIdsByRoleId(role.getRoleId());
            if(!permissionIds.isEmpty()){
                //根据所有的权限id查询所有的权限信息
                List<permission> permissions = permissionMapper.getPermissionsByList(permissionIds);
                role.setPermissions(permissions);
            }
        }

        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("roleList",roleList);
        resultMap.put("count",count);
        return resultMap;
    }

    @Override
    public Map<String, role> getAllRoleName() {
        List<role> roles = getRoles();
        Map<String,role> map = new LinkedHashMap<>();
        for (role s:roles){
            map.put(s.getRole(),s);
        }
        return map;
    }


    @Override
    public Boolean deleteRole(Integer roleId) {
        int i = roleMapper.deleteByPrimaryKey(roleId);
        if(i != 1){
            return false;
        }
        return true;
    }

    @Override
    public Boolean addRole(Map<String, String> map) {
        role role = new role();
        role.setRoleName(map.get("roleName").toString());
        int i = roleMapper.insert(role);
        if(i != 1){
            return false;
        }

        roleExample roleExample = new roleExample();
        cn.itcast.pojo.roleExample.Criteria criteria = roleExample.createCriteria();
        criteria.andRoleNameEqualTo(map.get("roleName").toString());
        Integer roleId = roleMapper.selectByExample(roleExample).get(0).getRoleId();
        String permissions = (String)map.get("permissionIds");
        String[] permissionIds = {};
        permissionIds = permissions.split(",");
        ArrayList<Integer> ids = new ArrayList<>();
        for(i=0 ; i<permissionIds.length ; i++){
            ids.add(Integer.parseInt(permissionIds[i]));
        }

        //开始给角色添加权限
        for (Integer id : ids) {
            int a = roleMapper.addRoleAndPermission(roleId, id);
            if(a != 1){
                return false;
            }
        }
        return true;
    }

    @Override
    public Boolean updateRole(Map<String, String> map) {
        role role = new role();
        int roleId = Integer.parseInt(map.get("roleId"));
        role.setRoleId(roleId);
        role.setRoleName(map.get("roleName").toString());
        int i = roleMapper.updateByPrimaryKeySelective(role);
        if(i != 1){
            return false;
        }

        //先删除该角色之前的所有权限，然后再添加前台修改页面选择的权限
        roleMapper.deleteRoleAndPermission(roleId);

        String permissions = map.get("permissionIds");
        String[] permissionIds = {};
        permissionIds = permissions.split(",");
        ArrayList<Integer> ids = new ArrayList<>();
        for(i=0 ; i<permissionIds.length ; i++){
            ids.add(Integer.parseInt(permissionIds[i]));
        }

        //开始给角色添加权限
        for (Integer id : ids) {
            int a = roleMapper.addRoleAndPermission(roleId, id);
            if(a != 1){
                return false;
            }
        }
        return true;
    }

    @Override
    public List<role> getRoles() {
        roleExample roleExample = new roleExample();
        List<role> roleList = roleMapper.selectByExample(roleExample);
        return roleList;
    }


}
